<template>
  <div class="login-account">
    <el-form
      label-width="60px"
      :rules="account_rules"
      :model="account"
      ref="ElFormRef"
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" type="password"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, defineExpose, ref } from 'vue';
import { ElForm } from 'element-plus';
import { account_rules } from '../config';
import { useStore } from 'vuex';
/* 导入本地缓存工厂函数 */
import localCache from '@/utils/localCache';
const ElFormRef = ref<InstanceType<typeof ElForm>>();
const store = useStore();
const account = reactive({
  name: localCache.getCache('name') ?? '',
  password: localCache.getCache('password') ?? ''
});
const loginAction = (isRememberPassword: boolean) => {
  ElFormRef.value?.validate((valid) => {
    //如果用户选了记住密码，就把用户信息保存在本地
    if (isRememberPassword) {
      localCache.setCache('name', account.name);
      localCache.setCache('password', account.password);
    } else {
      localCache.deleteCache('name');
      localCache.deleteCache('password');
    }
    //点击登录把用户名和密码发送给vuex的action，执行登录请求
    store.dispatch('login/accountLoginAction', { ...account });
  });
};
defineExpose({
  loginAction
});
</script>

<style scoped></style>
